<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2007 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: fastadmin: https://www.fastadmin.net/
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\CarModel;
use app\admin\model\College;
use app\admin\model\UserModel;
use app\common\controller\Adminbase;
use app\common\model\AliPay;
use app\common\model\BannerModel;
use app\common\model\ItemModel;
use app\common\model\DouyinModel;
use app\common\model\RebateLog;
use app\common\model\StaffModel;
use util\Tree;


class Item extends Adminbase
{

    protected function initialize()
    {
        parent::initialize();
        $this->modelClass = new \app\common\model\ItemModel();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($page, $limit, $where, $sort, $order) = $this->buildTableParames();

            $list = $this->modelClass
                ->withJoin(['cate','douyin','staff'])
                ->where($where)
                ->where('dy_id', '<>', '')
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $row) {
                $row->getRelation('cate')->visible(['cate_name']);
                $row->getRelation('staff')->visible(['username']);
                $row->getRelation('douyin')->visible(['nickname']);
            }

            $result = ["code" => 0, 'count' => $list->total(), "data" => $list->items()];
            return json($result);
        }
        $update_time = date('Y-m-d H:i:s', (new ItemModel())->order('id desc')->limit(1)->value('update_time'));
        $this->assign('update_time', $update_time);
        return $this->fetch();
    }
}
